'use strict';

const { Controller } = require('egg');
const sha1 = require('sha1');

class WechatController extends Controller {
  async check() {
    const obj = this.ctx.query;
    const token = this.ctx.app.config.wechat_config.token;
    const timestamp = obj.timestamp;
    const nonce = obj.nonce;
    const echostr = obj.echostr;
    const signature = obj.signature;
    const str = [ token, timestamp, nonce ].sort()
      .join('');
    const sha = sha1(str);
    if (sha === signature) {
      this.ctx.body = echostr + '';
    }
  }
}

module.exports = WechatController;
